package dao.impl;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public abstract class BaseDao {
	private static String driver;   //数据库驱动字符串
	private static String url;   //连接URL字符串
	private static String user;  //数据库用户名
	private static String password; //用户密码
	Connection connection = null;  //数据连接对象

	static {   //静态代码块,在类加载的时候执行
		init();
	}
	/**
	 * 初始化连接参数,从配置文件里获得
	 */
	public static void init() {
		Properties params = new Properties();
		String configFile = "database.properties";  //配置文件路径
		//加载配置文件到输入流中
		InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(configFile);
		try {
			//从输入流中读取属性列表
			params.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//根据指定的的获取对于的值
		driver = params.getProperty("driver");
		url = params.getProperty("url");
		user = params.getProperty("user");
		password = params.getProperty("password");
	}
	public Connection getConnection() {
		if(connection == null) {
			//获取连接并捕获
			try {
				Class.forName(driver);
				connection = DriverManager.getConnection(url,user,password);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return connection;
	}
	public void closeAll(Connection connection,Statement statement, ResultSet resultSet) {
		//若结果集对象不为空,则关闭
		if(resultSet != null) {
			try {
				resultSet.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		//若Statement对象不为空,则关闭
		if(statement != null) {
			try {
				statement.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		//若数据库连接对象不为空,则关闭
		if(connection != null) {
			try {
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	 /**
     * 增、删、改操作
     * 
     * @param sql
     *            sql语句
     * @param prams
     *            参数数组
     * @return 执行结果
     */
    protected int executeUpdate(String sql, Object... params) {
        int result = 0;
        connection = getConnection();
        PreparedStatement pstmt = null;
        try {
            pstmt = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
          closeAll(null, pstmt, null);
        }
        return result;
    }

    /**
     * 查询操作
     * 
     * @param sql
     *            sql语句
     * @param params
     *            参数数组
     * @return 查询结果集
     */
    protected ResultSet executeQuery(String sql, Object... params){
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        connection = getConnection();
        try {
            pstmt = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
    public abstract Object tableToClass(ResultSet rs) throws Exception;
}
